package cn.qyl.joha.controller.customer;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.qyl.joha.common.PageConstant;
import cn.qyl.joha.common.PageModel;
import cn.qyl.joha.dto.GoodsTypes;
import cn.qyl.joha.dto.Picturcarousel;
import cn.qyl.joha.dto.Timlimited;
import cn.qyl.joha.dto.User;
import cn.qyl.joha.service.PicService;
import cn.qyl.joha.service.TimeService;
import cn.qyl.joha.service.UserService;
import cn.qyl.joha.service.Impl.PicServiceImpl;
import cn.qyl.joha.service.Impl.TimeServiceImpl;
import cn.qyl.joha.service.Impl.UserServiceImpl;



@Controller
@RequestMapping("/customer")
public class UserController{
	
	//获取图片轮播业务层
	PicService picService = new PicServiceImpl();

	UserService userService = new UserServiceImpl();
	
	
	@RequestMapping("/home")
	public String home(ModelMap map){
		
		List<Picturcarousel> pics = picService.getPicsList();
		
		map.addAttribute("pics",pics);
		
		return"home";
	}
	
	@RequestMapping("/toLogin")
	public String toLogin(){
		return "login";
	}
	
	
	@RequestMapping("/userLogin")
	@ResponseBody
	public Map<String,String> login(String userId ,String pwd,String code,String rember,
			HttpSession session,HttpServletResponse response
			){
		Map<String,String> map = new HashMap<String, String>();
		UserService userService = new UserServiceImpl();
		//获取验证码
		String vcode  = (String) session.getAttribute(PageConstant.AUTH_CODE);
		if(vcode.equalsIgnoreCase(code)){
			//如果验证码相等 调用Service层获取session对象
			User user = userService.getUserLogin(userId,pwd);
			if(user != null){
				//把user对象存到session缓存
				session.setAttribute(PageConstant.SESSION_USER, user);
				//判断是否需要记住密码
				if("1".equals(rember)){
					Cookie cookie = new Cookie("userId", user.getName()+"-"+user.getPassWord());
					//setMaxAge():设置cookie的存活时间,单位:秒
					cookie.setMaxAge(60*60*24);
					//设置cookie的存储路径
					cookie.setPath("/");
					response.addCookie(cookie);
				}
				map.put("status", "success");
				return map;
			}else{
				//设置status状态
				map.put("status", "fail");
				map.put(PageConstant.TIP, "用户名或密码不正确");
				return map;
			}
			
		}else{
			map.put("status", "fail");
			map.put(PageConstant.TIP, "验证码不正确");
			return map;
		}
	}
	
	@RequestMapping("toRegister")
	public String toRegister(){
		
		return "register";
	}
	
	
	@RequestMapping("register")
	
	public String register(User user){
			
			userService.register(user);
			
		return "login";
			
	}
	

	
/*	@RequestMapping("getAjaxMenu")
	@ResponseBody
	public List<GoodsTypes> getAjaxMenu(){
		List<GoodsTypes> goodsTypesList =  goodsTypeService.getGoodsTypesMenu();
		return goodsTypesList;
	}*/
	
	//退出
	@RequestMapping("/loginOut")
	public String loginOut(HttpSession session){
		//注销用户
		session.removeAttribute(PageConstant.SESSION_USER);
		return "forward:home";
	}
}
